package com.miguo.db;

import android.content.Context;

import com.miguo.app.AppContext;
import com.miguo.bean.GoodsBean;
import com.miguo.db.GoodsDao.Properties;
import com.miguo.utils.DateUtils;

import java.util.List;

import de.greenrobot.dao.query.DeleteQuery;
import de.greenrobot.dao.query.QueryBuilder;

public class GoodsDB {
    private static Context mContext;
    private static GoodsDB instance;

    private GoodsDao goodsDao;

    private GoodsDB() {
    }

    public static GoodsDB getInstance(Context context) {
        if (instance == null) {
            instance = new GoodsDB();
            if (mContext == null) {
                mContext = context;
            }

            // 数据库对象
            DaoSession daoSession = AppContext.getDaoSession(mContext, false);
            instance.goodsDao = daoSession.getGoodsDao();
        }
        return instance;
    }

    public static GoodsDB getInstance(Context context, boolean ClearSession) {
        if (instance == null) {
            instance = new GoodsDB();
            if (mContext == null) {
                mContext = context;
            }

            // 数据库对象
            DaoSession daoSession = AppContext.getDaoSession(mContext, ClearSession);
            instance.goodsDao = daoSession.getGoodsDao();
        }
        return instance;
    }

    /**
     * 查询
     */

    public List<GoodsBean> loadAll() {
        return goodsDao.loadAll();
    }

    /**
     * insert or update note
     *
     * @param goods
     * @return insert or update note id
     */
    public long save(GoodsBean goods) {
        return goodsDao.insertOrReplace(goods);
    }

    /**
     * 查询
     */
    public boolean isSave(int Id) {
        QueryBuilder<GoodsBean> qb = goodsDao.queryBuilder();
        qb.where(Properties.Id.eq(Id));
        qb.buildCount().count();
        return qb.buildCount().count() > 0 ? true : false;
    }

    public boolean isSave(Long data, int type) {
        QueryBuilder<GoodsBean> qb = goodsDao.queryBuilder();
        if (type == 1) {
            qb.where(Properties.Item_id.eq(data));
            // qb.buildCount().count();
        } else if (type == 2) {
            qb.where(Properties.Begin_time.eq(data));
        }

        return qb.buildCount().count() > 0 ? true : false;
    }

    public int getNotiCountByStartTime(Long begin_time) {
        QueryBuilder<GoodsBean> qb = goodsDao.queryBuilder();
        qb.where(Properties.Begin_time.eq(begin_time));
        return (int) qb.buildCount().count();
    }

    /**
     * insert or update noteList use transaction
     *
     * @param list
     */
    public void saveList(final List<GoodsBean> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        goodsDao.getSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < list.size(); i++) {
                    GoodsBean note = list.get(i);
                    goodsDao.insertOrReplace(note);
                }
            }
        });

    }

    public void delete(GoodsBean goods) {
        goodsDao.delete(goods);
    }

    /**
     * 删除
     */
    public void delete(Long item_id, int type) {
        QueryBuilder<GoodsBean> qb = goodsDao.queryBuilder();
        if (type == 1) {
            DeleteQuery<GoodsBean> bd = qb.where(Properties.Item_id.eq(item_id))
                    .buildDelete();
            bd.executeDeleteWithoutDetachingEntities();
        }

    }

    /**
     * 删除
     */
    public void deleteGoodsList(int Id) {
        QueryBuilder<GoodsBean> qb = goodsDao.queryBuilder();
        DeleteQuery<GoodsBean> bd = qb.where(Properties.Id.eq(Id))
                .buildDelete();
        bd.executeDeleteWithoutDetachingEntities();
    }

    /**
     * delete all
     */
    public void deleteAll() {
        goodsDao.deleteAll();
    }

    /**
     * 通过id查找其类型id
     */
    public Long getTypeId(int cityId) {
        QueryBuilder<GoodsBean> qb = goodsDao.queryBuilder();
        qb.where(Properties.Id.eq(cityId));
        if (qb.list().size() > 0) {
            return qb.list().get(0).getItem_id();
        } else {
            return Long.valueOf(1);
        }
    }

    public String getNumIid() {
        String Data = ",";
        QueryBuilder<GoodsBean> qb = goodsDao.queryBuilder();
        if (qb.list() != null && qb.list().size() > 0) {
            for (int i = 0; i < qb.list().size(); i++) {
                Data += qb.list().get(i).getItem_id() + ",";
            }
        }
        return Data;
    }

    /**
     * query list with where clause
     * <p/>
     * ex: begin_date_time >= ? AND end_date_time<= ?
     *
     * @param where  where clause, include 'where' word
     * @param params query parameters
     * @return
     */

    public List<GoodsBean> query(String where, String... params) {
        return goodsDao.queryRaw(where, params);
    }

    public List<GoodsBean> query(int page, int pageSize, int type) {
        QueryBuilder<GoodsBean> qb = goodsDao.queryBuilder();
        Long Time = DateUtils.getTime();
        if (type == 1) {
            qb.where(Properties.Begin_time.ge(Time),
                    Properties.End_time.ge(Time));
        } else if (type == 2) {
            qb.where(Properties.Begin_time.le(Time),
                    Properties.End_time.ge(Time));
        } else if (type == 3) {
            qb.where(Properties.Begin_time.ge(Time),
                    Properties.End_time.gt(Time));
        }
        qb.orderDesc(Properties.Begin_time, Properties.Id);
        qb.orderAsc(Properties.End_time);
        qb.limit(pageSize);
        qb.offset((page - 1) * pageSize);
        return qb.list();
    }

    public List<GoodsBean> findRemind() {
        QueryBuilder<GoodsBean> qb = goodsDao.queryBuilder();
        Long Time = DateUtils.getTime();
        qb.where(Properties.Begin_time.ge(Time),
                Properties.End_time.gt(Time));
        qb.orderDesc(Properties.Begin_time, Properties.Id);
        qb.orderAsc(Properties.End_time);
        return qb.list();
    }


    public List<GoodsBean> getList(int Id) {
        QueryBuilder<GoodsBean> qb = goodsDao.queryBuilder();
        qb.where(Properties.Coupon_price.eq("time"), qb.or(
                Properties.End_time.gt("time"), qb.and(
                        Properties.Begin_time.eq("time"),
                        Properties.End_time.ge("time"))));
        return qb.list();
    }
}